<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="./js/jquery.min.js"></script>
    <script src="./js/common.js"></script>
    <style>
        .nav {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }

        .container {
            padding-top: 80px;
            height: auto;
        }

        /*.container-right {*/
        /*    width: auto;*/
        /*}*/

        .blog-pagnation-wrapper {
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }

        .blog-pagnation-item {
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }

        .blog-pagnation-item:hover {
            background: #4e4eeb;
            color: #fff;
        }

        .blog-pagnation-item.actvie {
            background: #4e4eeb;
            color: #fff;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/blog_logo.png" alt="">
        <a class="title" href="./myblog_list.html" id="to-my-blog">回到我的主页</a>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html" id="login_state">登录</a>
        <a href="myblog_list.html" id="common-list-my">我的</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div id="blog_content">

            </div>

            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="toFirstPage()">首页</button>
                <button class="blog-pagnation-item" onclick="toPreviousPage()">上一页</button>
                <button class="blog-pagnation-item" onclick="toNextPage()">下一页</button>
                <button class="blog-pagnation-item" onclick="toLastPage()">末页</button>
            </div>
        </div>
    </div>
    <script>
        var pageIndex = 1
        var pageSize = 2
        var pageCount = 0
        function getPageList(pageIndex, pageSize) {
            jQuery.ajax({
                url: "/art/limit",
                type: "post",
                data: {
                    "pageSize": pageSize,
                    "pageIndex": pageIndex
                },
                success: function (result) {
                    if (result != null && result.code == 200 && result.data.pageList.length > 0) {
                        var contentHtml = ''
                        for (var i = 0; i < result.data.pageList.length; i++) {
                            var blog = result.data.pageList[i]
                            contentHtml += `<div class="blog">
                    <div class="title">${blog.title}</div>
                    <div class="date">${blog.createTime}</div>
                    <div class="desc">
                        ${blog.content}
                    </div>
                    <a href="blog_content.html?id=${blog.artId}" class="detail">查看全文 &gt;&gt;</a>
                </div>`
                        }
                        jQuery('#blog_content').html(contentHtml)
                        pageCount = result.data.count
                    } else {
                        alert('文章信息获取失败，请刷新重试！')
                    }
                }
            })
        }
        function toFirstPage() {
            pageIndex = 1
            getPageList(pageIndex, pageSize)
        }
        function toPreviousPage() {
            if (pageIndex <= 1) {
                alert('已经是第一页了！')
                return
            }
            pageIndex = pageIndex - 1
            getPageList(pageIndex, pageSize)
        }
        function toNextPage() {
            if (pageIndex >= pageCount) {
                alert('这已经是最后一页了！')
                return
            }
            pageIndex = pageIndex + 1
            getPageList(pageIndex, pageSize)
        }
        function toLastPage() {
            pageIndex = pageCount
            getPageList(pageIndex, pageSize)
        }
        toFirstPage()


        function getLoginState() {
            jQuery.ajax({
                url: "user/is_login",
                type: "get",
                success: function (result) {
                    if(result != null && result.data == 'logined') {
                        jQuery('#login_state').text('注销')
                        jQuery('#login_state').attr('href','javascript:logout()')
                    }
                }
            })
        }
        getLoginState()
    </script>
</body>

</html>